<template>
  <div class="applet">
    <el-col v-bind="grid">
      <el-form ref="refOfficial" :model="modelOfficial" :rules="rulesOfficial" label-position="right" label-width="150px">
        <el-col :span="24">
          <el-form-item prop="appId">
            <template v-slot:label>AppID  <el-tooltip class="item" effect="dark" content="小程序AppID" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
            <el-input v-model="modelOfficial['appId']" placeholder="AppId" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="appSecret">
            <template v-slot:label>AppSecret  <el-tooltip class="item" effect="dark" content="小程序AppSecret" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
            <el-input v-model="modelOfficial['appSecret']" placeholder="AppSecret" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="name">
            <template v-slot:label>小程序名称  <el-tooltip class="item" effect="dark" content="小程序名称" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
            <el-input v-model="modelOfficial['name']" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item>
            <template v-slot:label>LOGO  <el-tooltip class="item" effect="dark" content="LOGO" placement="top-start"><i class="el-icon-info" /></el-tooltip></template>
            <div class="upLoadPicBox">
              <div v-if="modelOfficial.logoImage" class="pictrue">
                <el-image ref="LogoLargePreView" class="imgHover" :src="$uploadURL(modelOfficial.logoImage)" :preview-src-list="[$uploadURL(modelOfficial.logoImage)]"></el-image>
                <div class="upload_cover_preview">
                  <i @click.stop="largePreview('LogoLargePreView')" class="iconfont icon-yanjing"></i>
                  <i @click.stop="delLarge('logoImage')" class="el-icon l-upload el-icon-delete"></i>
                </div>
              </div>
              <div @click="onAppletClick" v-else class="upLoad"><i class="el-icon l-upload el-icon-camera-solid"></i></div>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" @click="onSubmit('refOfficial')">立即提交</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
  </div>
</template>

<script>
import { GETWechatApplet, PUTWechatApplet } from '@/api/setting';

export default {
  name: 'Applet',
  data() {
    return {
      grid: {
        xl: 7,
        lg: 9,
        md: 12,
        sm: 12,
        xs: 24
      },
      modelOfficial: {
        appId: '',
        appSecret: '',
        name: '',
        logoImage: ''
      },
      rulesOfficial: {
        appId: [
          { required: true, message: '请选择输入appId', trigger: 'blur' }
        ],
        appSecret: [
          { required: true, message: '请选择输入appSecret', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请选择输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.$nextTick(() => {
      // 获取小程序信息
      this.getApplet()
    })
  },
  methods: {
    // 删除图片
    delLarge(name) {
      this.$set(this.modelOfficial, name, '');
    },
    // 大图预览
    largePreview(ref) {
      this.$refs[ref].clickHandler();
    },
    onAppletClick() {
      const _this = this
      this.$modalUpload((data) => {
        _this.modelOfficial.logoImage = data[0].path
      }, '1')
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          PUTWechatApplet(this.modelOfficial).then(res => {
            this.getApplet()
            this.$message.success('修改成功！')
          })
        } else {
          this.$message.error('验证失败！')
          return false
        }
      })
    },
    getApplet() {
      GETWechatApplet().then(res => {
        this.$set(this, 'modelOfficial', res)
      }).catch(() => this.$message.error('获取小程序配置失败！'))
    }
  }
}
</script>

<style lang="scss" scoped>
  .applet {
    ::v-deep .viewCurrent {
      .l-del {
        font-size: 17px !important;
      }
    }
  }
</style>
